﻿using DapperExtensions;
using Restaurant.Common;
using Restaurant.Dao;
using Restaurant.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Restaurant.Business
{
    public class SignInBusiness
    {
        readonly StaffDao staffDao = new StaffDao();
  
        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="subCompanyId"></param>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task<OperationResult> SignIn(Guid subCompanyId, string uid, string pwd)
        { 
            //where
            IList<IPredicate> predList = new List<IPredicate>();
            predList.Add(Predicates.Field<Staff>(p => p.SubCompany_Id, Operator.Eq, subCompanyId));
            predList.Add(Predicates.Field<Staff>(p => p.UId, Operator.Eq, uid));
            IPredicateGroup where = Predicates.Group(GroupOperator.And, predList.ToArray());

            var r = await staffDao.GetListAsync(where);

            if (null == r)
                return new OperationResult(OperationResultType.QueryNull, "数据不存在");

            if (r.Item2 == 1)
            {
                var m = r.Item1.ToList()[0];
                if (m.PassWord == pwd)
                    return new OperationResult(OperationResultType.Success, "成功", m);
                else
                    return new OperationResult(OperationResultType.ParamError, "用户名或密码错误");

            }

            if (0 == r.Item2)
                return new OperationResult(OperationResultType.ParamError, "用户名或密码错误");

            if (r.Item2 > 1)
                return new OperationResult(OperationResultType.Error, "数据异常");
            else
                return new OperationResult(OperationResultType.QueryNull, "数据不存在");

        }

    }
}
